package com.jxc.action.dls;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VAgencyGrantRecord;
import com.jxc.service.AgencyServiceI;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/dls")
@Action(value = "agencyAction", results = {
		@Result(name = "agency", location = "/agency/agency.jsp"),
		@Result(name = "agencyAdd", location = "/agency/agencyAdd.jsp") })
public class AgencyAction extends BaseAction implements
		ModelDriven<VAgencyGrantRecord> {

	@Autowired
	private AgencyServiceI agencyService;
	private VAgencyGrantRecord model = new VAgencyGrantRecord();

	@Override
	public VAgencyGrantRecord getModel() {
		return model;
	}

	public String agency() {
		getSubAuths("WDDLS_WDDLS");
		return "agency";
	}

	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		model.setDid(sessionInfo.getUserId());
		writeJson(agencyService.datagrid(model));
	}

	public String agencyAdd() {
		return "agencyAdd";
	}

	public void add() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		model.setDid(sessionInfo.getUserId());
		Integer result = agencyService.save(model);
		Json j = new Json();
		if (result == 1) {
			j.setMsg("添加失败，已经授权过！");
			j.setSuccess(false);
		} else {
			j.setMsg("添加成功！");
			j.setSuccess(true);
		}
		super.writeJson(j);
	}

}
